<template>
  <el-container
    style="height: 700px; border: 1px solid #eee"
    :style="'background-image:url(' + Background + ');'"
  >
    <div
      style="
        height: 500px;
        border: 1px solid #eee;
        background-color: #f4f5f5;
        text-align: center;
        margin-left: 5%;
      "
    >
      <el-switch
        v-model="isAdmin"
        active-color="#13ce66"
        active-text="管理员"
        inactive-color="#ff4949"
        inactive-text="普通用户"
      >
      </el-switch>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        label-position="right"
        class="login-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code" v-if="isAdmin != true">
          <el-input v-model="ruleForm.code" autocomplete="off"></el-input>
          <div class="input-group-addon">
            <img class="captcha" src="/auth/captcha" name="captcha" />
            <el-button @click="updateCaptcha()">看不清，换一张</el-button>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="toRegister()">没有账号,前往注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-container>
</template>

<script>
import router from "@/router/routers";
import { login, AdminLogin } from "@/api/auth";
import Background from "@/assets/images/background.jpg";
import { Notification } from "element-ui";

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    return {
      Background: Background,
      isAdmin: false,
      CodeImg: null,
      ruleForm: {
        username: "",
        password: "",
        email: "",
        code: "",
      },
      rules: {
        password: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },
  // mounted() {
  //   // console.log(this.$route.query.redirect);
  //   // router.push({ path: "/index" });
  // },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.isAdmin == true) {
            // 管理员登录
            AdminLogin(this.ruleForm.username, this.ruleForm.password).then(
              (res) => {
                if (res) {
                  localStorage.setItem("ID", res);
                  localStorage.setItem("NAME", this.ruleForm.username);
                  // 页面跳转;
                  if (this.$route.query.redirect != undefined) {
                    console.log(this.$route.query.redirect);
                    router.push({ path: this.$route.query.redirect });
                  } else {
                    router.push({ path: "/dashboard/admin" });
                    // router.push({ path: "/index" });
                  }
                }
              }
            );
          } else {
            // 普通用户登录
            login(
              this.ruleForm.username,
              this.ruleForm.password,
              this.ruleForm.code
            ).then((res) => {
              console.log(this.ruleForm.code);
              if (res.code > 0) {
                localStorage.setItem("UID", res.data);
                localStorage.setItem("NAME", this.ruleForm.username);
                // 页面跳转;
                console.log("完成");
                if (this.$route.query.redirect != undefined) {
                  console.log(this.$route.query.redirect);
                  router.push({ path: this.$route.query.redirect });
                } else {
                  // router.push({ path: "/index" });
                  location.href = "/index";
                }
              } else {
                Notification.error({
                  title: res.msg,
                  duration: 5000,
                });
                this.updateCaptcha();
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toRegister() {
      router.push({ path: "/register" });
    },
    /**
     * 更新验证码
     */
    updateCaptcha() {
      document.images.captcha.src = "/auth/captcha?t" + Math.random();
    },
  },
};
</script>

<style>
.login-ruleForm {
  margin-left: 5%;
  margin-top: 100px;
  margin-right: 5%;
}
</style>